/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.platform.viewers;

import com.patientis.model.common.IBaseModel;

import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Text;


/**
 *
 * SymbiosysTextControlFocusListener.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class SymbiosysTextControlFocusListener implements FocusListener {
    public final static String MODEL = "model";
    protected final int REF_ID;

    public SymbiosysTextControlFocusListener(int _refId) {
        REF_ID = _refId;
    }

    public void focusGained(final FocusEvent _fe) {
    }

    public void focusLost(final FocusEvent _fe) {
        Text _txt = (Text) (_fe.widget);

        String _input = _txt.getText().trim();
        IBaseModel _mdl = (IBaseModel) _txt.getParent()
                                           .getData(SymbiosysTextControlFocusListener.MODEL);

        Long modelRefId;

        try {
            IBaseModel _subMdl = (IBaseModel) _txt.getData(SymbiosysTextControlFocusListener.MODEL);

            if (_subMdl != null) {
                modelRefId = _subMdl.getId();
            } else {
                String _ref = Integer.toString(REF_ID).trim();
                modelRefId = Long.parseLong(((Integer) _fe.widget
                                             .getData(_ref)).toString().trim());
            }

            //      Change the model's value
            _mdl.setValue(Integer.parseInt(modelRefId.toString().trim()), _input);

            //      set the model data back into the control
            _txt.getParent()
                .setData(SymbiosysTextControlFocusListener.MODEL, _mdl);
        } catch (Exception _e) {
            _e.printStackTrace();

            return;
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
	//
    }
}
